package com.aaa.emp.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * fileName:MyRibbonConfiguration
 * description:
 * author:zz
 * createTime:2020/7/22 11:12
 * version:1.0.0
 */
@Configuration   //<beans></beans>
public class MyRibbonConfiguration {

    /**
     * ribbon借助RestTemplate实现功能，实例化RestTemplate
     * 并且开启负载均衡功能
     * @return
     */
    @Bean  //<bean id=restTemplate class='org.springframework.web.client.RestTemplate'>
    @LoadBalanced  //开启负载均衡功能  默认情况下就是轮询
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 定义负载均衡的类型 (相当于使用了随机(MyRandomRule)的方式)(轮询RoundRobinRule)
     * @return
     */
    @Bean
    public IRule iRule(){
        return new RoundRobinRule();
    }
}
